package com.heima.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.heima.common.dto.PageResponseResult;
import com.heima.common.dto.ResponseResult;
import com.heima.common.enums.AppHttpCodeEnum;
import com.heima.user.dto.ApAuthor;
import com.heima.user.dto.AuthDto;
import com.heima.user.dto.WmUser;
import com.heima.user.entity.ApUser;
import com.heima.user.entity.ApUserRealname;
import com.heima.user.feign.ArticleClient;
import com.heima.user.feign.MediaClient;
import com.heima.user.mapper.ApUserRealnameMapper;
import com.heima.user.service.IApUserRealnameService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.user.service.IApUserService;
import io.seata.spring.annotation.GlobalTransactional;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * APP实名认证信息表 服务实现类
 * </p>
 *
 * @author mcm
 * @since 2022-08-07
 */
@Service
public class ApUserRealnameServiceImpl extends ServiceImpl<ApUserRealnameMapper, ApUserRealname> implements IApUserRealnameService {

    //按照状态查询用户认证列表
    @Autowired
    private IApUserService userService;

    @Autowired
    private MediaClient mediaClient;

    @Autowired
    private ArticleClient articleClient;

    @Override
    public ResponseResult listByStatus(AuthDto dto) {
        //创建分页条件
        IPage<ApUserRealname> page = new Page<>(dto.getPage(), dto.getSize());
        //构建查询条件
        LambdaQueryWrapper<ApUserRealname> qw = new LambdaQueryWrapper<>();
        //根据状态查询
        if (dto.getStatus() != null) {
            qw.eq(ApUserRealname::getStatus, dto.getStatus());
        }
        IPage<ApUserRealname> iPage = this.page(page, qw);
        //返回数据
        return new PageResponseResult(
                dto.getPage(),
                dto.getSize(),
                iPage.getTotal(),
                iPage.getRecords()
        );
    }

    /**
     * 认证审核
     * @param dto
     * @param type  1:通过   0:驳回
     * @return
     */
    @Override
    @GlobalTransactional
    public ResponseResult auth(AuthDto dto, int type) {
        //根据id查询认证表数据
        ApUserRealname userRealname = this.getById(dto.getId());
        //失败 修改用户认证状态为2  保存失败的原因
        if (type == 0) {
            userRealname.setStatus(2);
            userRealname.setReason(dto.getReason());
            this.updateById(userRealname);
            return ResponseResult.okResult();
        }
        // 审核成功
        // 查询ap_user
        ApUser apUser = userService.getById(userRealname.getUserId());
        // 创建自媒体账号 注册信息从ap_user同步
        WmUser wmUser = createWmUser(apUser);
        // 创建作者账号
        if (wmUser != null) {
            ApAuthor author = createAuthor(apUser, wmUser);
            if (author != null) {
                // 认证表的状态修改为9 审核通过
                userRealname.setStatus(9);
                this.updateById(userRealname);
                // 修改用户注册表的flag 为1 自媒体人
                apUser.setFlag(1);
                userService.updateById(apUser);
            }
            return ResponseResult.okResult();
        }

        return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
    }


    /**
     * 创建自媒体用户
     *
     * @param apUser
     * @return
     */
    private WmUser createWmUser(ApUser apUser) {
        // 构建自媒体用户
        WmUser wmUser = new WmUser();
        // 基本属性从apUser复制
        BeanUtils.copyProperties(apUser, wmUser);
        wmUser.setId(null);
        wmUser.setApUserId(apUser.getId());
        wmUser.setNickname(apUser.getName());
        wmUser.setStatus(9);
        wmUser.setType(0);
        // 远程调用自媒体接口
        ResponseResult<WmUser> responseResult = mediaClient.saveWmUser(wmUser);
        // 判断调用的结果
        if (responseResult.getCode().equals(AppHttpCodeEnum.SUCCESS.getCode())) {
            WmUser user = responseResult.getData();
            return user;
        }
        return null;
    }

    /**
     * 创建文章作者
     *
     * @param apUser
     * @param wmUser
     * @return
     */
    private ApAuthor createAuthor(ApUser apUser, WmUser wmUser) {
        // 构建文章作者
        ApAuthor author = new ApAuthor();
        author.setName(apUser.getName());
        author.setType(2);
        author.setUserId(apUser.getId());
        author.setWmUserId(wmUser.getId());
        // 远程调用文章服务接口
        ResponseResult<ApAuthor> responseResult = articleClient.saveApAuthor(author);
        // 判断操作成功
        if (responseResult.getCode().equals(AppHttpCodeEnum.SUCCESS.getCode())) {
            // 返回作者数据
            ApAuthor apAuthor = responseResult.getData();
            // 作者的id写回到自媒体用户表中
            wmUser.setApAuthorId(apAuthor.getId());
            mediaClient.updateWmUser(wmUser);
            System.err.println(2222);
            return apAuthor;
        }
        return null;
    }
}






















